package com.niit.web.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.niit.bean.NoteRepo;
import com.niit.bean.User;
import com.niit.service.NoteRepoService;
import com.niit.service.NoteService;
import com.niit.service.impl.NoteRepoServiceImpl;
import com.niit.service.impl.NoteServiceImpl;
import com.niit.utils.WebKit;

@WebServlet("/DeleteNoteRepoController")
public class DeleteNoteRepoController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NoteRepo repo = WebKit.request2Bean(request, NoteRepo.class);
		NoteService ns = new NoteServiceImpl();
		NoteRepoService nrs = new NoteRepoServiceImpl();
		User user = (User) request.getSession().getAttribute("user");

		OutputStream os = response.getOutputStream();
		if(ns.isNoteIn(user, repo)){
			os.write("该笔记本中存有笔记，不能删除".getBytes());
		}else{
			if(nrs.delete(repo)>0){
				os.write("删除成功".getBytes());
			}else{
				os.write("删除失败".getBytes());
			}
		}
	}

}
